package com.chenfwind.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chenfwind.IDao.OwnerMapper;
import com.chenfwind.IDao.PermissionMapper;
import com.chenfwind.IDao.RoleMapper;
import com.chenfwind.IDao.TrajectoryMapper;
import com.chenfwind.IDao.UserMapper;
import com.chenfwind.IDao.UserTrackMapper;
import com.chenfwind.common.StringUtils;
import com.chenfwind.domain.Owner;
import com.chenfwind.domain.Permission;
import com.chenfwind.domain.Role;
import com.chenfwind.domain.Trajectory;
import com.chenfwind.domain.User;
import com.chenfwind.domain.UserTrack;
import com.chenfwind.domain.vo.UserVO;
import com.chenfwind.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService{

	@Autowired
	private UserMapper userDao;
	
	@Autowired
	private PermissionMapper permissionDao;
	
	@Autowired
	private RoleMapper roleDao;
	
	@Autowired
	private UserTrackMapper userTrackDao;
	
	@Autowired
	private OwnerMapper ownerDao;
	
	@Autowired
	private TrajectoryMapper trajectoryDao;
	
	@Transactional
	public void register(User user){
		userDao.insertSelective(user);
	}
	
	/*
	 * 通过用户id获取用户对象信息
	 * 
	 */
	@Transactional
	public User findUserById(String id) {
		return (User)userDao.selectByPrimaryKey(id);
	}
	
	/****
	 * 通过用户名获取用户对象
	 * 
	 * @param username
	 * @return
	 */
	@Transactional
	public User getUserByAccount(String account) {
		User user = (User)userDao.selectByAccount(account);
		return user;
	}

	/****
	 * 登录时，获取对象，来验证用户
	 * 
	 * @param account
	 * @param password
	 * @return 
	 */
	@Transactional
	public User getUserByAccountAndPassword(String account , String password){
		User user = (User)userDao.selectByAccountAndPassword(account, password);
		return user;
	}
	
	/***
	 * 通过用户名获取权限资源
	 * 
	 * @param username
	 * @return
	 */
	@Transactional
	public List<String> getPermissionByAccount(String account) {
		System.out.println("调用");
		User user = getUserByAccount(account);
		if ( user == null ){
			return null;
		}
		
		List<String> list = new ArrayList<String>();
		for ( Role role : user.getRole()){
			List<Permission> permissionList  = permissionDao.selectAllPermissionByRoleId(role.getId());
			for(Permission p : permissionList){
				list.add(p.getUrl());
			}
		}
		return list ;
	}
	
	@Transactional
	public int modifyUserInfo(User user,UserVO userVO){
		if (userVO.getX() != null && userVO.getY() != null) {
//			修改原本的点，使其的状态变成0
			userTrackDao.updateStatus(0,user.getId());
			
//			创建新坐标点
			UserTrack userTrack = new UserTrack();
			userTrack.setUserId(userVO.getId());
			userTrack.setX(Double.valueOf(userVO.getX()));
			userTrack.setY(Double.valueOf(userVO.getY()));
			userTrack.setId(StringUtils.genUUID());
			userTrack.setTrackname(userVO.getTrackName());
			userTrack.setRank(userTrackDao.selectByUserID(userTrack.getUserId()).size() + 1);
			userTrack.setStatus(1);
			userTrackDao.insertSelective(userTrack);
			
			//修改所有该用户的书籍，并更新他的坐标
			List<Owner> owenrList = ownerDao.selectByUserID(userVO.getId(), 1) ;
			for (Owner owner :owenrList) {
				trajectoryDao.updateStatusByBookId(owner.getBookId(),0);
				
				Trajectory tra = new Trajectory();
				tra.setId(StringUtils.genUUID());
				tra.setBookId(owner.getBookId());
				tra.setUserId(owner.getUserId());
				tra.setStatus(1);
				tra.setRank(trajectoryDao.selectMaxRank(owner.getBookId()) + 1);
				tra.setX(Double.valueOf(userVO.getX()));
				tra.setY(Double.valueOf(userVO.getY()));
				tra.setBooktrackname(userVO.getTrackName());
				trajectoryDao.insertSelective(tra);
			}
		}
		return userDao.updateByPrimaryKeySelective(user);
	} 
	
	public String getAccountByUID(String userId){
		return userDao.selectByPrimaryKey(userId).getAccount();
	}
	
	public User getUser(String account){
		return userDao.selectUserRoleByAccount(account);
	}
	
	public Role getRole(String name){
		return roleDao.selectUserRoleByName(name);
	}
	
	public List<Permission> getPermission(String roleId){
		return permissionDao.selectAllPermissionByRoleId(roleId);
	}
	
	public List<User> getAllUser(){
		return userDao.getAllUser();
	}
	
	public int delUser(String userId){
		return userDao.delUser(userId);
	}
}
